<template>
    <div class="unit_form_container">
        <el-form ref="unitForm" :rules="rules" :model="formData" label-width="100px">
            <el-form-item label="名称" prop="name">
                <el-input v-model="formData.name" autocomplete="off" placeholder="请输入名称" />
            </el-form-item>
            <el-form-item label="排序" prop="sn">
                <el-input v-model="formData.sn" v-int-new autocomplete="off" placeholder="请输入排序" />
            </el-form-item>
            <el-form-item label="启用" prop="state">
                <el-switch v-model="formData.state" :active-value="1" :inactive-value="0" />
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import base from "@common/components/dialog/base.js";

export default {
    extends: base,
    name: "UnitForm",
    data() {
        return {
            rules: {
                code: [
                    { required: true, message: "请输入编码", trigger: "blur" },
                    {
                        pattern: /^[A-Za-z0-9+-_.]{1,32}$/,
                        message:
                            "最大32位字符，大小写英文、数字或者以下特殊字符（+-_.），不能包含特殊字符",
                    },
                ],
                name: [
                    { required: true, message: "请输入名称", trigger: "blur" },
                    //目前只兼容中文、大小写英文、数字
                    {
                        // pattern: /^[A-Za-z0-9\u4e00-\u9fa5]{1,50}$/, message:'最大 50 位大小写英文、数字或者其它地区语言字符，不能包含特殊字符'
                        max: 50,
                        message: "最大 50 位字符，内容过长，请删减",
                        trigger: "blur",
                    },
                ],
            },
            formData: {},
        };
    },
    methods: {
        init() {
            const _this = this;
            if (_this.propsData.init) {
                return;
            }
            if (_this.$refs.unitForm) {
                _this.$refs.unitForm.clearValidate();
            }
            _this.formData = $.extend({}, _this.propsData.row);
        },
        async confirm() {
            const _this = this;
            let valid = await _this.validateForm('unitForm');
            if (valid) {
                let subData = $.extend({}, _this.formData);
                return _this.fetchData(_this.$api.API_SaveBaseUnit, subData).then(res => {
                    _this.$message.success('保存成功！');
                    if (_this.selectBottom) {
                        _this.$emit('event', 'closeDialog')
                        _this.$emit('event', { type: 'refresh', data: res.data })
                    }
                    return true;
                }).catch(() => {
                    return false;
                });
            }
            return valid;
        },
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.unit_form_container {
    width: 100%;
    height: 100%;
    overflow: hidden;
}
</style>
